/*
 * Project Name spring-boot-tools
 * File Name BaseMessage
 * Package Name com.huxiaosu.tools.mina.model
 * Create Time 2024/5/25
 * Create by name：liujie
 */
package com.huxiaosu.tools.mina.model;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.Serializable;

/**
 * Description
 *
 * @author liujie
 * @date 2024/5/25 13:36
 */
@Slf4j
@Data
public class BaseMessage implements Serializable {
    private byte type;
    private Integer           bodyLength;
    private byte[]            body;
    public byte[] toByte(){
        ByteArrayOutputStream bous = new ByteArrayOutputStream();
        DataOutputStream dous = new DataOutputStream(bous);
        try {
            dous.writeByte(type);
            dous.writeInt(body.length);
            dous.write(body);
            dous.close();
        } catch (IOException e) {
            log.error("封装链接二进制数组失败。"+e);
        }
        return bous.toByteArray();
    }
}
